package cn.Service.Impl;

import cn.Dao.EmployeeDao;
import cn.Entity.Employee;
import cn.Service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class EmployeeServiceImpl implements IEmployeeService {

//    @Resource(name = "employeeDao")
//    @Autowired
    private EmployeeDao employeeDao;

    public EmployeeServiceImpl(EmployeeDao ed){
        this.employeeDao = ed;
    }
    public EmployeeServiceImpl(){}

    @Override
    public void setEmployeeDao(EmployeeDao ed){
        this.employeeDao = ed;
    }

    @Override
    @Transactional
    public void batchDeleteEmployee(int begin , int end){
        for (int i = begin; i < end; i++) {
            Employee temp = this.employeeDao.selectOne(i);
            if(temp==null){
                throw new IllegalStateException("没有此员工！");
            }
            this.employeeDao.deleteOne(i);
        }
    }

    @Override
    @Cacheable(value = "allEmployee" , key = "#root.methodName")
//    @Cacheable(value = "employeeCache" , key = "#root.methodName")
    public List<Employee> findAll() {
        System.out.println("Access Database ..." + new Date().toString());
        return this.employeeDao.findAll();
    }

    @Override
    public void saveEmployee(Employee employee) {
        this.employeeDao.addOne(employee);
    }

    @Override
    @Cacheable(value = "employeeCache" , key = "#id")
    public Employee getOne(Integer id){
        System.out.println("Access Database ... : employeeId："+id);
        return this.employeeDao.selectOne(id);
    }

}
